using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class BgmBtnImageCtrl : MonoBehaviour
{
    
    // [Header("bgm暂停恢复按钮")]
    // public GameObject bgm_btn_comp;

    [Header("Pause Image")]
    public Sprite img_0;
    
    [Header("UnPause Image")]
    public Sprite img_1;

    private BdTools.TimeIntervalUtil tt;

    private Button btn_comp;
    
    // Start is called before the first frame update
    void Start()
    {
        BdUtils.assert(img_0 && img_1, "img_0和img_1均不能为空!");
        // BdUtils.assert(img_0 != null, "img_0和img_1均不能为空!");
        // BdUtils.assert(img_1 != null, "img_0和img_1均不能为空!");

        tt = new BdTools.TimeIntervalUtil(0.1f);

        btn_comp = gameObject.GetComponent<Button>();
    }

    AudioSource bgm_audio_source_comp
    {
        get
        {
            return AudioVolumeManager.instance.bgm_audio_source_comp;
        }
    }

    // Update is called once per frame
    void Update()
    {
        if (bgm_audio_source_comp == null || bgm_audio_source_comp.clip == null)
        {
            btn_comp.interactable = false;
            return;
        }
        else
        {
            btn_comp.interactable = true;
        }
        
        if (img_0 == null || img_1 == null)
            return;

        if (!tt.is_time_to_run())
            return;

        var btn_image = gameObject.GetComponent<Image>();
        
        // 获取已经播放了多久
        float playedTime = bgm_audio_source_comp.time;

        // 获取背景音乐的总长度
        float totalLength = bgm_audio_source_comp.clip.length;

        // Debug.Log("已播放时间: " + playedTime + " / 总长度: " + totalLength);
    
        // BdUtils.log("playing --- ", bgm_audio_source_comp.isPlaying, $"已播放时间: {playedTime.ToString("F1")} / 总长度: " + totalLength);
        
        if (bgm_audio_source_comp.isPlaying)
        {
            btn_image.sprite = img_0;
            // btn_image.sprite = img_0.sprite;
        }
        else
        {
            btn_image.sprite = img_1;
            // btn_image.sprite = img_1.sprite;
        }
    }
}
